<template>
    <headerTitle title="商家信息"></headerTitle>
    <!-- 商家logo部分 -->
    <div class="business-logo">
        <img :src="business.businessImg">
    </div>
    <!-- 商家信息部分 -->
    <div class="business-info">
        <h1>{{ business.businessName }}</h1>
        <p>&#165;{{ business.starPrice }}起送 &#165;{{ business.deliveryPrice }}配送</p>
        <p>{{ business.businessExplain }}</p>
    </div>

    <!-- 食品列表部分 -->
    <ul class="food" v-if="foodsInfo != null">
        <li v-for="(food, index) in foodsInfo" :key="index">
            <div class="food-left">
                <img :src="food.foodImg">
                <div class="food-left-info">
                    <h3>{{ food.foodName }}</h3>
                    <p>{{ food.foodExplain }}</p>
                    <p>&#165;{{ food.foodPrice }}</p>
                </div>
            </div>
            <div class="food-right">
                <div @click="subFood(index)">
                    <i :class="[food.quantity > 0 ? 'fa fa-plus-circle' : 'fa fa-minus-circle']"></i>
                </div>
                <p><span>{{ food.quantity }}</span></p>
                <div @click="addFood(index)">
                    <i class="fa fa-plus-circle"></i>
                </div>
            </div>
        </li>
    </ul>

    <!-- 购物车部分 -->
    <div class="cart">
        <div class="cart-left">
            <div class="cart-left-icon">
                <i class="fa fa-shopping-cart"></i>
                <div class="cart-left-icon-quantity" v-show="getCartLen > 0">{{ getCartLen }}</div>
            </div>
            <div class="cart-left-info">
                <p>&#165;{{ getTotalPrice }}</p>
                <p>另需配送费{{ business.deliveryPrice }}元</p>
            </div>
        </div>
        <div class="cart-right">
            <!-- 达到起送费 -->
            <div class="cart-right-item" v-if='getTotalPrice > business.starPrice'
                @click="router.push({ name: 'confirmOrderPage', query: { id: business.businessId } })">去结算</div>

            <!-- 不够起送费 -->
            <div class="cart-right-item" v-else>&#165;{{ business.starPrice }}起送</div>
            
        </div>
    </div>
</template>

<script setup>
import headerTitle from '@/components/headerTitle.vue';
import { useRouter, useRoute } from 'vue-router'
import { ref, computed } from 'vue'
import { getBussinessRequest, getFoodListRequest, getCartRequest, addFood8CartRequest, subFood8CartRequest } from '@/js/request';
let router = useRouter()
let route = useRoute()
let business = ref({
    businessId: null,
    businessName: null,
    businessAddress: null,
    businessExplain: null,
    businessImg: null,
})
let foodsInfo = ref([])
let addFood = (index) => {
    addFood8CartRequest(respon => {
        if (respon.data.code == 200) {
            foodsInfo.value[index].quantity++
        } else if (respon.data.code == 400 && respon.data.detail.errorCode == 401) {
            if (confirm("需先登录")) {
                router.push({ name: 'loginPage' })
            }
        }
    }, business.value.businessId, foodsInfo.value[index].foodId)
}
let subFood = (index) => {
    subFood8CartRequest(respon => {
        if (respon.data.code == 200) {
            foodsInfo.value[index].quantity--
        } else if (respon.data.code == 400 && respon.data.detail.errorCode == 401) {
            if (confirm("需先登录")) {
                router.push({ name: 'loginPage' })
            }

        }
    }, business.value.businessId, foodsInfo.value[index].foodId)
}
getBussinessRequest((respon) => {
    if (respon.data.code == 200) {
        business.value = respon.data.detail
    }else if(respon.data.code == 701){
        alert("无该商家")
        router.push({ name: 'root' })
    }
}, route.query.id)
getFoodListRequest((respon) => {
    if (respon.data.code == 200) {
        foodsInfo.value = respon.data.detail
        getCart()
    }
}, route.query.id)
let getCart = () => getCartRequest((respon) => {
    if (respon.data.code == 200) {
        respon.data.detail.foods.forEach((element) => {
            for (var i = 0; i < foodsInfo.value.length; i++) {
                if (foodsInfo.value[i].foodId == element.foodId) {
                    foodsInfo.value[i].quantity = element.quantity

                }
            }
        })
    }
}, route.query.id)

let getCartLen = computed(() => {
    var n = 0
    foodsInfo.value.forEach(item => {
        if (item.quantity > 0)
            n++
    })
    return n

})

let getTotalPrice = computed(() => {
    var sum = 0;
    foodsInfo.value.forEach(item => {
        sum += item.foodPrice * item.quantity
    })
    return sum

})
</script>

<style lang="scss" scoped>
/****************** 商家logo部分 ******************/
.business-logo {
    width: 100%;
    height: 35vw;
    /*使用上外边距避开header部分*/
    margin-top: 12vw;

    display: flex;
    justify-content: center;
    align-items: center;
}

.business-logo img {
    width: 40vw;
    height: 30vw;
    border-radius: 5px;
}

/****************** 商家信息部分 ******************/
.business-info {
    width: 100%;
    height: 20vw;

    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.business-info h1 {
    font-size: 5vw;
}

.business-info p {
    font-size: 3vw;
    color: #666;
    margin-top: 1vw;
}

/****************** 食品列表部分 ******************/
.food {
    width: 100%;
    /*使用下外边距避开footer部分*/
    margin-bottom: 14vw;
}

.food li {
    width: 100%;
    box-sizing: border-box;
    padding: 2.5vw;
    user-select: none;

    display: flex;
    justify-content: space-between;
    align-items: center;
}

.food li .food-left {
    display: flex;
    align-items: center;
}

.food li .food-left img {
    width: 20vw;
    height: 20vw;
}

.food li .food-left .food-left-info {
    margin-left: 3vw;
}

.food li .food-left .food-left-info h3 {
    font-size: 3.8vw;
    color: #555;
}

.food li .food-left .food-left-info p {
    font-size: 3vw;
    color: #888;
    margin-top: 2vw;
}

.food li .food-right {
    width: 16vw;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.food li .food-right .fa-minus-circle {
    font-size: 5.5vw;
    color: #999;
    cursor: pointer;
}

.food li .food-right p {
    font-size: 3.6vw;
    color: #333;
}

.food li .food-right .fa-plus-circle {
    font-size: 5.5vw;
    color: #0097EF;
    cursor: pointer;
}

/****************** 购物车部分 ******************/
.cart {
    width: 100%;
    height: 14vw;

    position: fixed;
    left: 0;
    bottom: 0;

    display: flex;
}

.cart .cart-left {
    flex: 2;
    background-color: #505051;
    display: flex;
}

.cart .cart-left .cart-left-icon {
    width: 16vw;
    height: 16vw;
    box-sizing: border-box;
    border: solid 1.6vw #444;
    border-radius: 8vw;
    background-color: #3190E8;
    font-size: 7vw;
    color: #fff;

    display: flex;
    justify-content: center;
    align-items: center;

    margin-top: -4vw;
    margin-left: 3vw;

    position: relative;
}

.cart .cart-left .cart-left-icon-quantity {
    width: 5vw;
    height: 5vw;
    border-radius: 2.5vw;
    background-color: red;
    color: #fff;
    font-size: 3.6vw;

    display: flex;
    justify-content: center;
    align-items: center;

    position: absolute;
    right: -1.5vw;
    top: -1.5vw;
}

.cart .cart-left .cart-left-info p:first-child {
    font-size: 4.5vw;
    color: #fff;
    margin-top: 1vw;
}

.cart .cart-left .cart-left-info p:last-child {
    font-size: 2.8vw;
    color: #AAA;
}

.cart .cart-right {
    flex: 1;
}

/*达到起送费时的样式*/
.cart .cart-right .cart-right-item {
    width: 100%;
    height: 100%;
    background-color: #38CA73;
    color: #fff;
    font-size: 4.5vw;
    font-weight: 700;
    user-select: none;
    cursor: pointer;

    display: flex;
    justify-content: center;
    align-items: center;
}

/*不够起送费时的样式（只有背景色和鼠标样式的区别）*/
/*
.cart .cart-right .cart-right-item{
width: 100%;
height: 100%;
background-color: #535356;
color: #fff;
font-size: 4.5vw;
font-weight: 700;
user-select: none;

display: flex;
justify-content: center;
align-items: center;
}
*/
</style>